package com.tensquare.article.service;
/*
 *ClassName:ArticleService1
 *Description:
 *@author:可爱的大鸭子
 */

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.client.UserClient;
import com.tensquare.article.dao.ArticleDao1;
import com.tensquare.article.pojo.Article;
import com.tensquare.article.pojo.Article1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import util.IdWorker;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class ArticleService1 {
    @Autowired
    private ArticleDao1 articleDao;
    @Autowired
    private UserClient userClient;
    @Autowired
    private IdWorker idWorker;
    //查询所有
    public List<Article1> findAll() {
        return articleDao.selectList(null);
    }
    //根据id查询
    public Article1 findById(String id) {
        return articleDao.selectById(id);
    }
    //添加
    public void add(Article1 article) {
        article.setId(idWorker.nextId() + "");
        articleDao.insert(article);
    }
    //更新
    public void update(Article1 article) {
        //根据id号更新
        //方法1
        articleDao.updateById(article);
        //方法2
        EntityWrapper wrapper = new EntityWrapper<Article>();
        wrapper.eq("id", article.getId());
        articleDao.update(article, wrapper);
    }
    //删除
    public void delete(String id) {
        articleDao.deleteById(id);
    }
    //查找分页
    public Page search(Map map, int page, int size) {
        EntityWrapper wrapper = new EntityWrapper<Article>();
        Set<String> fieldSet = map.keySet();
        for(String field : fieldSet) {
            //wrapper.eq(field, map.get(field));
            wrapper.eq(null != map.get(field), field, map.get(field));
        }
        Page page1 = new Page(page, size);
        List<Article1> list = articleDao.selectPage(page1, wrapper);
        for(Article1 article:list){
            HashMap data = (HashMap) userClient.findById(article.getUserid()).getData();
            article.setAvatar(data.get("avatar").toString());
            article.setNickname(data.get("nickname").toString());
        }
        page1.setRecords(list);
        return page1;
    }
}
